package document;

import java.rmi.RemoteException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import replication.RegServer_Replication;
import rmi.Server;



public class AutoSave extends TimerTask{
	List<Document> userDocs;
	Timer t;
	Server s;
	RegServer_Replication repl;
	int userId;

	public AutoSave(List<Document> userDocs, Timer t, Server s, int userId, RegServer_Replication r) {
		this.userDocs = userDocs;
		this.t = t;
		this.s = s;
		this.userId = userId;
		this.repl = r;
		// TODO Auto-generated constructor stub
	}

	@Override
	public void run() {
		
		for (Document doc : userDocs) {
			
			// text chat doc
			if (doc.getDocId() == 1000) {
				continue;
			}
			
			try {
				s.saveDoc(userId, doc.getDocId(), doc.getContentBox().getText());
			} catch (RemoteException r) {
				try {
					s = repl.getServer();
					s.saveDoc(userId, doc.getDocId(), doc.getContentBox().getText());
				} catch (Exception e) {repl.shutDown();}
			}
		}
	}
}
